﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoUpdater
{
    class Program
    {
        static void Main(string[] args)
        {
           

            DBTransactions trans = new DBTransactions();
            
            List<AutoSell> mysell= trans.getAllAutoSellStocks();
            stockDataQuery qry = new stockDataQuery();
            Random rnd = new Random();
            while(mysell.Count>0)
            {
                int rndval = rnd.Next(0, mysell.Count);
                //randomize the sell
                
                float price = qry.getCurrentPrice(mysell.ElementAt(rndval).getsymbol());
                //market price is greate than setted sell price.
                if (price > mysell.ElementAt(rndval).getPrice())
                {
                    //do sell
                    Stock stk = new Stock(mysell.ElementAt(rndval).getsymbol(), price, mysell.ElementAt(rndval).getShare());
                    User usr = new User(mysell.ElementAt(rndval).getLogin());
                    
                    trans.updateStokAfterSell(usr, stk, stk.SharesOutstanding);
                    trans.creditBalance(usr, stk, stk.SharesOutstanding);
                    trans.deductandAddbrokerFees(mysell.ElementAt(rndval).getLogin());
                    trans.insertIntoHistory(usr, stk, price, "AUTOSELL");
                    AutoSell sellx = new AutoSell();
                    sellx = mysell.ElementAt(rndval);
                    trans.deleteFromAutoSell(sellx);
                    
                }
                
                
                mysell.RemoveAt(rndval);
               
               
            }
            trans.DeletefromUserStock();
           
            List<AutoBuy> mybuy = trans.getAllAutoBuyStocks();
            rnd = new Random();
            while (mybuy.Count > 0)
            {
                //before buying check balance;
                int rndval = rnd.Next(0, mybuy.Count);

                float price = qry.getCurrentPrice(mybuy.ElementAt(rndval).getstksymbol());
                if (price < mybuy.ElementAt(rndval).getBuyPrice() && price >= 0)
                {
                    Stock stk = new Stock(mybuy.ElementAt(rndval).getstksymbol(), price, mybuy.ElementAt(rndval).getShare());
                    User usr = new User(mybuy.ElementAt(rndval).getUser());
                    float balance = trans.retrieveAccountBal(usr.getUserID());

                    if (balance > stk.SharesOutstanding * stk.Price)
                    {
                        //do insert into userstock
                        trans.inIntoUserStock(usr, stk, stk.SharesOutstanding);
                        trans.debitBalance(usr, stk, (float)stk.SharesOutstanding);
                        trans.deductandAddbrokerFees(mybuy.ElementAt(rndval).getUser());
                        trans.insertIntoHistory(usr, stk, stk.Price, "AUTOBUY");
                        trans.removeFromAutoBuy(mybuy.ElementAt(rndval));
                        
                    }

                    
                }
                mybuy.RemoveAt(rndval);
            }
           

        }
    }
}
